/*
 * @Descripttion:
 * @version: V1.0
 * @Author: ZYP  2305592180@qq.com
 * @Date: 2025-05-27 09:50:48
 * @LastEditors: ZYP
 * @LastEditTime: 2025-11-04 10:38:37
 */
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
// #include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#include "driver/sdmmc_host.h"
#include "esp_log.h"
#include "sdmmc_common.h"
#include "esp_err.h"
#include "led.h"
#include "mmc.h"
#include "zyp_usb.h"
#include "esp_task_wdt.h"
#include "zyp_log.h"
#include "freertos/FreeRTOS.h"
#include "app_main.h"
#include "esp_system.h"
#include "driver/i2c.h"
#include "_i2c.h"
// sdmmc_card_t *card = NULL;
file_transfer_state_t file_current_state = LLF_FILE_STATE;
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();

void zyp_cid_task(void *pvParameters)
{

    zyp_read_cid(card);
    while (1)
    {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void zyp_virgin_flow_task(void *pvParameters)
{
#if IS_EN_SEMAPHORE
    /// WATING  Semaphore FREE
    if (xSemaphoreTake(xInitSemaphore, 1000 / portTICK_PERIOD_MS) != pdTRUE)
    {
        // ZYP_DEBUG("Semaphore take fail");
        // return;
        vTaskDelay(pdMS_TO_TICKS(50));
    }
#endif
    // esp_task_wdt_add(NULL);
    esp_err_t err;
    while (1)
    {
#if EN_VRIGINL_DEBUG
        ZYP_DEBUG("zyp_virgin_flow_task\n");
        esp_task_wdt_reset();
        vTaskDelay(pdMS_TO_TICKS(1000)); ////here influce the vrginal process    when printf zyp_usb: All files received.   but vrginal task is not exctution
        esp_task_wdt_reset();
#endif
        if (current_file_index - 1 == LLF_FILE && file_current_state == LLF_FILE_STATE)
        {
            ZYP_DEBUG("LLF_FILE  recive\n");
            file_current_state = DNLD_FILE_STATE;
        }
        if (current_file_index - 1 == DNLD_FILE && file_current_state == DNLD_FILE_STATE)
        {
            ZYP_DEBUG("DNLD file  recive\n");
            file_current_state = MAIN_FILE_STATE;
        }
        if (current_file_index - 1 == MAIN_FILE && file_current_state == MAIN_FILE_STATE)
        {
            ZYP_DEBUG("MAIN file  recive\n");
            err = zyp_virgin_flow(card, current_file_index - 1);
            /// if host computer send file file_current_state will be change！！！！  todo though tinyminUSB send  vriginl  triumph  to host computer
            if (err == ESP_OK && file_current_state == MAIN_FILE_STATE) /// MAIN_FILE_STATE sure  vriginl  at monment suceess send if vriginl successs
            {
            }
            else if (err != ESP_OK && file_current_state == MAIN_FILE_STATE)
            {
#if IS_ENABLE_FACTORY_MODE_PROCESS
                ZYP_DEBUG("err message recive \n");
                factory_mode_t = zyp_construct_vriginal_message_fream(factory_mode_t);
                if (xQueueSend(usb_factory_mode_queue, &factory_mode_t, pdMS_TO_TICKS(50)) != pdTRUE)
                {
                    ZYP_DEBUG("Failed to send to usb_factory_mode_queue\n");
                }
                else
                {
                    ZYP_DEBUG(" err message: send success\n");
                }
#endif
            }
            file_current_state = LLF_FILE_STATE;
            current_file_index = 0;
        }
        vTaskDelay(pdMS_TO_TICKS(1000)); /// if  take out  err freertos sholud not has return   esp32 continuous return

        if (current_file_index == SCIRIPT_FILE && debugbinfile_reciveover == true)
        {
#if IS_FILE_DEBUG
            err = zyp_debugbin_flow(card, current_file_index);
            virigin_file_size_t.debug_file_size = 0;
            debug_file_index_startbit = 0;
            current_file_index = LLF_FILE;
#endif            
        }

        // vTaskDelay(pdMS_TO_TICKS(2000));               q ///thingking delay 2s virgial cmd  will not send   why?
        // ZYP_DEBUG("zyp_virgin_flow_task has done\n");
    }
}


#if NO_ENABLE_TASK
/**
 * @name:
 * @msg:
 * @return {*}
 */
void zyp_usb_task_debug(void *pvParameters)
{
#if 1
    // esp_task_wdt_add(NULL); // add watch_dog watch
#endif
    while (1)
    {
        vTaskDelay(pdMS_TO_TICKS(7000));
        esp_task_wdt_reset(); /// Feed Dog
        zyp_send_file_via_usb(current_file_index - 1);
    }
}
#endif


void zyp_emmc_init_task(void *pvParameters)
{
#if ENABLE_TASK
    // esp_task_wdt_add(NULL);
#endif

    // led_init();
    esp_err_t ret;
    ZYP_DEBUG("------------------------------Initializing host------------------------------\n");

    // card->is_mmc = 1;
    // card->ocr = 0x40FF8000;  // supoort volatge 2.7-3.6V
    // By default, eMMC frequency is initialized to SDMMC_FREQ_DEFAULT (20MHz)
    // For setting a specific frequency, use host.max_freq_khz (range 400kHz - 52MHz for SDMMC)
    // Example: for fixed frequency of 10MHz, use host.max_freq_khz = 10000;
    sdmmc_host_t host = SDMMC_HOST_DEFAULT();
    host.slot = SDMMC_HOST_SLOT_0;
    host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
    // host.max_freq_khz = 500;                                                           /// no work
    // sdmmc_host_set_card_clk(card->host.slot,500);                                   ///err

    // This initializes the slot without card detect (CD) and write protect (WP) signals.
    // Other fields will be initialized to zero
    slot_config.cd = SDMMC_SLOT_NO_CD; /// Use to signal not connected to S/W
    slot_config.wp = SDMMC_SLOT_NO_WP;
#if 1
    // Set bus width to use:
#if CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
    slot_config.width = 8;
#elif CONFIG_EXAMPLE_EMMC_BUS_WIDTH_4
    slot_config.width = 4;
#else
    slot_config.width = 1;
#endif
    // Set bus IOs
#if CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
    slot_config.clk = CONFIG_EXAMPLE_PIN_CLK;
    slot_config.cmd = CONFIG_EXAMPLE_PIN_CMD;
    slot_config.d0 = CONFIG_EXAMPLE_PIN_D0;
#if CONFIG_EXAMPLE_EMMC_BUS_WIDTH_4 || CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
    slot_config.d1 = CONFIG_EXAMPLE_PIN_D1;
    slot_config.d2 = CONFIG_EXAMPLE_PIN_D2;
    slot_config.d3 = CONFIG_EXAMPLE_PIN_D3;
#endif // CONFIG_EXAMPLE_EMMC_BUS_WIDTH_4 || CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
#if CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
    slot_config.d4 = CONFIG_EXAMPLE_PIN_D4;
    slot_config.d5 = CONFIG_EXAMPLE_PIN_D5;
    slot_config.d6 = CONFIG_EXAMPLE_PIN_D6;
    slot_config.d7 = CONFIG_EXAMPLE_PIN_D7;
#endif // CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
#endif // CONFIG_SOC_SDMMC_USE_GPIO_MATRIX

    // Enable internal pullups on enabled pins. The internal pullups
    // are insufficient however, please make sure 10k external pullups are
    // connected on the bus. This is for debug / example purpose only.
    slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP; ///  在启用的引脚上开启内部上拉电阻。芯片内部的上拉电阻可能不够强（不足以保证信号稳定），建议在总线上外接 10K 欧姆的上拉电阻。  为所使用的 SDMMC 引脚启用内部上拉电阻。   仅仅软件设置哟，实际还是需要接10k的上拉电阻
#endif

#if 1
    ret = zyp_host_initialize_only(&host, &slot_config, &card, &cmd);
    if (ret != ESP_OK)
    {
        ESP_LOGE("EMMC_INIT", "eMMC initialization failed: %s", esp_err_to_name(ret));
        return;
    }
    ESP_LOGI("EMMC_INIT", "eMMC initialized successfully");
    // there  card_srtuct  has benn constrcut CID、CSD deflut value
    if (card != NULL) ///
    {
        ESP_LOGI(TAG, "Manufacturer ID: %02x", card->cid.mfg_id);
        ESP_LOGI(TAG, "Product Name: %c%c%c%c%c",
                 card->cid.name[0], card->cid.name[1],
                 card->cid.name[2], card->cid.name[3],
                 card->cid.name[4]);
        ESP_LOGI(TAG, "Capacity: %d", card->csd.capacity);
        ZYP_DEBUG("card->is_mmc:%d\n", card->is_mmc);
    }

#endif
    ZYP_DEBUG("Initializing host done\n");
#if 0
    sdmmc_init_host_frequency(card);        ///emmc
    zyp_init_mmc(&host,card);
#endif

#if NOT_EN_EMMC_INIT_WRITE_READ
    // zyp_cmd61(card);
    // zyp_cmd0_go_idle_state(card);
    // zyp_init_mmc(&host,card);
    zyp_init_mmc_random(card);
    // Card has been initialized, print its properties

    ZYP_DEBUG("-----------------------------crad_is_inint-----------------------------\n");
    zyp_sdmmc_card_print_info(stdout, card);

    // zyp_sdmmc_configure_bus_fre_mode(card,SDMMC_FREQ_PROBING,4,0);        ///write ok read  the third block data crc
    // zyp_sdmmc_configure_bus_fre_mode(card,SDMMC_FREQ_DEFAULT,8,0);     ///8bit sdr
    // zyp_mmc_random_write_read(card);

    ZYP_DEBUG("-----------------------------set hos bus fre  emmc-----------------------------\n");
    zyp_sdmmc_card_print_info(stdout, card);
    vTaskDelete(NULL);
#endif

    while (1)
    {
        // zyp_init_mmc_random(card);
        // ZYP_DEBUG("-----------------------------crad_is_inint-----------------------------\n");
        // zyp_sdmmc_card_print_info(stdout, card);
        // zyp_sdmmc_configure_bus_fre_mode(card, SDMMC_FREQ_PROBING, 1, 0); /// write ok read  the third block data crc
        // zyp_mmc_random_write_read(card);
        // zyp_sdmmc_configure_bus_fre_mode(card, SDMMC_FREQ_HIGHSPEED, 4, 0); /// write ok read  the third block data crc
        // zyp_mmc_packed_write(cmd, card, &requestbuffer, &responsebuffer, address_w, block_counts_w, sizeof(address_w) / sizeof(address_w[0]), &write_data[0]);

#if IS_EN_SEMAPHORE
        ////// release semaphore, inform other this task over other task can run
        xSemaphoreGive(xInitSemaphore);
#endif
        vTaskDelete(NULL);
    }
}


void IN219_task(void *pvParameters)
{

#if EN_ABLE_GP6_7    
    ZYP_DEBUG("----------------------GP6 7------------------------------\n");
    esp_err_t err = zyp_i2c_master_init(ZP_I2C_MASTER_NUM_1, GPIO_NUM_6, GPIO_NUM_7);  // GP6 CH0 SDA      SCL CH1 
    uint8_t device_addresses[] = {0x40, 0x41};
    int num_devices = sizeof(device_addresses) / sizeof(device_addresses[0]);
 #else   
    ZYP_DEBUG("----------------------GP8 9------------------------------\n");
    esp_err_t err = zyp_i2c_master_init(ZP_I2C_MASTER_NUM_1, GPIO_NUM_8, GPIO_NUM_9);     // GPO8 CH2 SDA    gp9  SCL CH3
    uint8_t device_addresses[] = {0x40, 0x41, 0x44, 0x45};
    int num_devices = sizeof(device_addresses) / sizeof(device_addresses[0]);
#endif


    if (err != ESP_OK)
    {
        ZYP_DEBUG("I2C 2 INIT ERR\n");
        i2c_driver_delete(ZP_I2C_MASTER_NUM_1);
        return;
    }
    else
    {
        ZYP_DEBUG("I2C 2 INIT OK\n");
    }

    enhanced_i2c_scanner(ZP_I2C_MASTER_NUM_1);


#if IS_EN_SEMAPHORE
    for (int i = 0; i < num_devices; i++) {
        esp_err_t ret = ina219_init_third(ZP_I2C_MASTER_NUM_1, device_addresses[i]);
        if (ret != ESP_OK) {
            ZYP_DEBUG("Failed to configure INA219 at address 0x%02X\n", device_addresses[i]);
        } else {
            ZYP_DEBUG("Successfully configured INA219 at address 0x%02X\n", device_addresses[i]);
        }
    }
    ZYP_DEBUG("All devices configured\n");
    while (1) {
        for (int i = 0; i < num_devices; i++) {
            uint16_t voltage_data_16;
            uint16_t current_data_16;
            uint16_t power_data_16;
            
            ZYP_DEBUG("------------0x%02X:\n----------", device_addresses[i]);
            
            esp_err_t ret_voltage = ina219_get_bus_voltage_third(ZP_I2C_MASTER_NUM_1, device_addresses[i], &voltage_data_16);
            if (ret_voltage == ESP_OK) {
                ZYP_DEBUG("INA219 voltage: %.1f mV\n", (double)voltage_data_16);
            }
            
            esp_err_t ret_current = ina219_get_current_third(ZP_I2C_MASTER_NUM_1, device_addresses[i], &current_data_16);
            if (ret_current == ESP_OK) {
                ZYP_DEBUG("INA219 Current: %.1f mA\n", (double)current_data_16);
            }
            
            esp_err_t ret_power = ina219_get_power_third(ZP_I2C_MASTER_NUM_1, device_addresses[i], &power_data_16);
            if (ret_power == ESP_OK) {
                ZYP_DEBUG("INA219 power: %.1f mW\n", (double)power_data_16);
            }
        }
        
        ZYP_DEBUG("\n");
        vTaskDelay(pdMS_TO_TICKS(5000));
    }
#else
    esp_err_t ret = (ZP_I2C_MASTER_NUM_1,0x41);       ///slave addr 0x49, config reg： 00   05  （量程）  写 00 05 config add
    if (ret != ESP_OK)
    {
        ZYP_DEBUG("Failed to configure INA219\n");
         return;
    }
    ZYP_DEBUG("write_register ok\n");
    uint16_t voltage_data_16;
    uint16_t power_data_16;
    uint16_t current_data16;
    ZYP_DEBUG("------------40:\n----------");
    ina219_get_bus_voltage_third(ZP_I2C_MASTER_NUM_1,0x40,&voltage_data_16);
    ZYP_DEBUG("INA219 voltage_data_16: %.1f mV\n", (double)voltage_data_16);
    ina219_get_current_third(ZP_I2C_MASTER_NUM_1,0x40,&current_data16);
    ZYP_DEBUG("INA219 Current: %.1f mA\n", (double)current_data16);
    ina219_get_power_third(ZP_I2C_MASTER_NUM_1,0x40,&power_data_16);
    ZYP_DEBUG("INA219 power_data_16: %.1f mW\n", (double)power_data_16);

     ZYP_DEBUG("------------41:\n----------");
    ina219_get_bus_voltage_third(ZP_I2C_MASTER_NUM_1,0x41,&voltage_data_16);
    ZYP_DEBUG("INA219 voltage_data_16: %.1f mV\n", (double)voltage_data_16);
    ina219_get_current_third(ZP_I2C_MASTER_NUM_1,0x41,&current_data16);
    ZYP_DEBUG("INA219 Current: %.1f mA\n", (double)current_data16);
    ina219_get_power_third(ZP_I2C_MASTER_NUM_1,0x41,&power_data_16);
    ZYP_DEBUG("INA219 power_data_16: %.1f mW\n", (double)power_data_16);

    ZYP_DEBUG("------------44:\n----------");
    ina219_get_bus_voltage_third(ZP_I2C_MASTER_NUM_1,0x44,&voltage_data_16);
    ZYP_DEBUG("INA219 voltage_data_16: %.1f mV\n", (double)voltage_data_16);
    ina219_get_current_third(ZP_I2C_MASTER_NUM_1,0x44,&current_data16);
    ZYP_DEBUG("INA219 Current: %.1f mA\n", (double)current_data16);
    ina219_get_power_third(ZP_I2C_MASTER_NUM_1,0x44,&power_data_16);
    ZYP_DEBUG("INA219 power_data_16: %.1f mW\n", (double)power_data_16);

    ZYP_DEBUG("------------45:\n----------");
    ina219_get_bus_voltage_third(ZP_I2C_MASTER_NUM_1,0x45,&voltage_data_16);
    ZYP_DEBUG("INA219 voltage_data_16: %.1f mV\n", (double)voltage_data_16);
    ina219_get_current_third(ZP_I2C_MASTER_NUM_1,0x45,&current_data16);
    ZYP_DEBUG("INA219 Current: %.1f mA\n", (double)current_data16);
    ina219_get_power_third(ZP_I2C_MASTER_NUM_1,0x45,&power_data_16);
    ZYP_DEBUG("INA219 power_data_16: %.1f mW\n", (double)power_data_16);

    // vTaskDelay(pdMS_TO_TICKS(1000));
    // while (1)
    // {
    //     zyp_test_i2c_read_register(ZP_I2C_MASTER_NUM_1,0x41, 0, 0, 0);
    //     vTaskDelay(pdMS_TO_TICKS(1000));
    // }

#endif
}


void creat_psram_potter_and_Request_FactoryMode_Queen_and_init_tud_usb(void)
{
#if IS_ENABLE_INIT
    psram_base = (uint8_t *)heap_caps_malloc(FILE_SLOT_SIZE, MALLOC_CAP_SPIRAM); /// alloc addr 0x3c030a88   every reset esp32 alloc  psram_base addr is random?
    if (!esp_ptr_external_ram(psram_base))
    {
        ZYP_DEBUG("The address %p is not in external RAM\n", psram_base);
        return;
    }
    /// psram_base addr
    ZYP_DEBUG("heap_caps_malloc psram_base = %p\n", psram_base); /// The allocated address space  0x3c030a88
    if (!psram_base)
    {
        ESP_LOGE(TAG, "Failed to allocate memory for rx_buf");
        return;
    }
    else
    {
        ESP_LOGE(TAG, "SUCCESS allocate memory for rx_buf");
    }
    ZYP_DEBUG("-----------------------------TINY_USB init -----------------------------\n");
    init_tud_usb_usart_psram();

#if ENABLE_TASK
    // usb_debug_queue = xQueueCreate(uxQueueLength,sizeof(struct RwReciveBuffer));        ///Solve the dynamic management of uncertain bytes in received data
    usb_cmd_queue = xQueueCreate(uxQueueLength, sizeof(struct RequestBuffer));
    if (usb_cmd_queue == NULL)
    {
        ZYP_DEBUG("Failed to create  cmd queue\n");
        return;
    }
    ZYP_DEBUG(" usb_cmd_queue has done\n");
#endif

#if IS_ENABLE_FACTORY_MODE_PROCESS
    usb_factory_mode_queue = xQueueCreate(uxQueueLength, sizeof(FactoryMode_T));
    if (usb_factory_mode_queue == NULL)
    {
        ZYP_DEBUG("Failed to create  cmd queue\n");
        return;
    }
    ZYP_DEBUG(" usb_factory_mode_queue has done\n");
#endif

#if IS_EN_SEMAPHORE
    xInitSemaphore = xSemaphoreCreateBinary();
    if (xInitSemaphore == NULL)
        ZYP_DEBUG("Failed to create semaphore\n");
#endif
#endif
}

void craete_emmc_App_task(void)
{
#if ENABLE_TASK
    xTaskCreate(zyp_emmc_init_task, "eMMC Init Task", 4096, NULL, tskIDLE_PRIORITY + 1, NULL);
#endif

#if NO_ENABLE_TASK
    xTaskCreate(zyp_usb_task_debug, "USB Task", 8192, NULL, tskIDLE_PRIORITY + 1, NULL);
#endif

#if ENABLE_TASK
    ZYP_DEBUG("------------------------------zyp_virgin_flow_task  begain -----------------------------\n");
    xTaskCreate(zyp_virgin_flow_task, "eMMC virgin task", 8192, NULL, tskIDLE_PRIORITY + 1, NULL);
#endif

#if IS_ENABLE_CMD_DATA_DEBUG
    xTaskCreate(USB_CMD_TASK, "USB CMD Task", 8192, NULL, tskIDLE_PRIORITY + 1, NULL); /// recive task   send task in usb_call_back function
    usb_data_queue = xQueueCreate(uxQueueLength, sizeof(struct RwReciveBuffer));
    if (usb_data_queue == NULL)
    {
        ZYP_DEBUG("Failed to create data cmd queue\n");
        return;
    }
#endif



#if NO_ENABLE_TASK
    ZYP_DEBUG("------------------------------cid task  begain -----------------------------\n");
    xTaskCreate(zyp_cid_task, "cid task", 4096, NULL, tskIDLE_PRIORITY, NULL);
#endif
}

void app_main(void)
{
#if DEFINE_EMMC
    creat_psram_potter_and_Request_FactoryMode_Queen_and_init_tud_usb();
    craete_emmc_App_task();
#else
    xTaskCreate(IN219_task, "in219 task", 4096, NULL, tskIDLE_PRIORITY + 1, NULL);
#endif

#if 1


#if 0
    zyp_led_init();
    EMMC_GPIO_init(GPIO_NUM_21);
    vTaskDelay(5000);
    EMMC_POWEROF(GPIO_NUM_21);
    esp_err_t ret;
    ZYP_DEBUG("Initializing host\n");
#endif

///single thread run init emmc host
#if 0
    sdmmc_card_t *card = NULL;
    // card->is_mmc = 1;
    // card->ocr = 0x40FF8000;  // supoort volatge 2.7-3.6V
    // By default, eMMC frequency is initialized to SDMMC_FREQ_DEFAULT (20MHz)
    // For setting a specific frequency, use host.max_freq_khz (range 400kHz - 52MHz for SDMMC)
    // Example: for fixed frequency of 10MHz, use host.max_freq_khz = 10000;
    sdmmc_host_t host = SDMMC_HOST_DEFAULT();
    host.slot = SDMMC_HOST_SLOT_0;
    host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;

    // This initializes the slot without card detect (CD) and write protect (WP) signals.
    // Other fields will be initialized to zero
    sdmmc_slot_config_t slot_config = {
        .cd = SDMMC_SLOT_NO_CD, /// /*!< Use to signal not connected to S/W */
        .wp = SDMMC_SLOT_NO_WP,
    };

#if 1
    // Set bus width to use:
#if CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
    slot_config.width = 8;
#elif CONFIG_EXAMPLE_EMMC_BUS_WIDTH_4
    slot_config.width = 4;
#else
    slot_config.width = 1;
#endif
    // Set bus IOs
#if CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
    slot_config.clk = CONFIG_EXAMPLE_PIN_CLK;
    slot_config.cmd = CONFIG_EXAMPLE_PIN_CMD;
    slot_config.d0 = CONFIG_EXAMPLE_PIN_D0;
#if CONFIG_EXAMPLE_EMMC_BUS_WIDTH_4 || CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
    slot_config.d1 = CONFIG_EXAMPLE_PIN_D1;
    slot_config.d2 = CONFIG_EXAMPLE_PIN_D2;
    slot_config.d3 = CONFIG_EXAMPLE_PIN_D3;
#endif // CONFIG_EXAMPLE_EMMC_BUS_WIDTH_4 || CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
#if CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
    slot_config.d4 = CONFIG_EXAMPLE_PIN_D4;
    slot_config.d5 = CONFIG_EXAMPLE_PIN_D5;
    slot_config.d6 = CONFIG_EXAMPLE_PIN_D6;
    slot_config.d7 = CONFIG_EXAMPLE_PIN_D7;
#endif // CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
#endif // CONFIG_SOC_SDMMC_USE_GPIO_MATRIX

    // Enable internal pullups on enabled pins. The internal pullups         在启用的引脚上开启内部上拉电阻。
    // are insufficient however, please make sure 10k external pullups are    芯片内部的上拉电阻可能不够强（比如不足以保证信号稳定），因此建议在总线上外接 10K 欧姆的上拉电阻。
    // connected on the bus. This is for debug / example purpose only.
    slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP; /// 为所使用的 SDMMC 引脚启用内部上拉电阻。
#endif

#if 1
    esp_err_t ret = zyp_host_initialize_only(&host, &slot_config, &card, &cmd);
    {
        if (ret != ESP_OK)
        {
            ZYP_DEBUG("EMMC_INIT eMMC initialization failed: %s", esp_err_to_name(ret));
            return;
        }
    }

    ESP_LOGI("EMMC_INIT", "eMMC initialized successfully");
    // 此时 card 结构体已经填充了 eMMC 的 CID、CSD 等信息
    ESP_LOGI(TAG, "Manufacturer ID: %02x", card->cid.mfg_id);
    ESP_LOGI(TAG, "Product Name: %c%c%c%c%c",
             card->cid.name[0], card->cid.name[1],
             card->cid.name[2], card->cid.name[3],
             card->cid.name[4]);
    ESP_LOGI(TAG, "Capacity: %d", card->csd.capacity);
    ZYP_DEBUG("card->is_mmc:%d\n", card->is_mmc);
#endif
    ZYP_DEBUG("Initializing host done\n");

#if 0
    sdmmc_init_host_frequency(card);        ///emmc
    zyp_init_mmc(&host,card);
#endif

#if 1
    // zyp_cmd61(card);
    // zyp_cmd0(card);
    // zyp_init_mmc(&host,card);
    zyp_init_mmc_random(card);            /// init must be do
    ZYP_DEBUG("%d\n", __LINE__);
    // Card has been initialized, print its properties

    ZYP_DEBUG("-----------------------------crad_is_inint-----------------------------\n");
    ////// test diffrtent bus hs read write
    // zyp_sdmmc_card_print_info(stdout, card);
    zyp_sdmmc_configure_bus_fre_mode(card, SDMMC_FREQ_PROBING, 1, 0); /// write ok read  the third block data crc
    // zyp_sdmmc_configure_bus_fre_mode(card, SDMMC_FREQ_HIGHSPEED, 8, 0); /// 8bit sdr  20MHZ      though test host switch max is 40MHZ ，despite switch  SDMMC_FREQ_52M
    // zyp_mmc_random_write_read(card);

    ///cmd23 cmd25 avoid

#if 0
    uint8_t *data = (uint8_t *)malloc(512);
    memset(data, 0x55, 512);
    sdmmc_command_t cmd_set_cnt = {
        .opcode = MMC_SET_BLOCK_COUNT,
        .arg = (3 & 0XFFFF),
        .flags = SCF_CMD_AC | SCF_RSP_R1};
    if (!zyp_mmc_set_blocklcnt(&cmd_set_cnt, card))
    {
        ZYP_DEBUG_CASE("err setblock:\n");
    }
    requestbuffer.cmdId = MMC_WRITE_BLOCK_MULTIPLE;
    requestbuffer.cmdParm = 0;
    esp_err_t err = zyp_mmc_write_sectors(cmd, card, &requestbuffer, data,
                                       0, 3);
    ZYP_DEBUG_MMC("err:%s\n", esp_err_to_name(err));
#endif


    ///test packed_write
    // zyp_mmc_packed_write(cmd, card, &requestbuffer, &responsebuffer, address_w, block_counts_w, sizeof(address_w) / sizeof(address_w[0]), &write_data[0]);
    
    ///test packed read
    // zyp_mmc_packed_read(cmd, card, &requestbuffer, &responsebuffer, address_w, block_counts_w, sizeof(address_w) / sizeof(address_w[0]));
    
    /// test cmddq write
#if 1
    // zyp_mmc_create_cmdq_task_write(cmd,card,&requestbuffer,&responsebuffer,taskId[0],number_block[1],lba[0]);
    ZYP_DEBUG("write watting task1\n");
    vTaskDelay(pdMS_TO_TICKS(1000));
    // zyp_mmc_create_cmdq_task_write(cmd,card,&requestbuffer,&responsebuffer,taskId[1],number_block[10],lba[1]);
#endif

#endif

#endif


#endif
}
